﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Kokomo.Environment
{
	/// <summary>
	/// Represents a filter that matches a specific header line.
	/// </summary>
	/// <remarks>
	/// The filter compares the first line of the header to <see cref="HeaderLine"/>.
	/// </remarks>
	public class FileTypeHeaderLineFilter : FileTypeFilter
	{
		/// <summary>
		/// Initializes a new <see cref="FileTypeHeaderLineFilter"/>.
		/// </summary>
		/// <param name="fileType">The <see cref="FileType"/> associated with the filter.</param>
		/// <param name="headerLine">The header line to match.</param>
		public FileTypeHeaderLineFilter(
			FileType fileType,
			string headerLine
			)
			: base(fileType)
		{
			if (string.IsNullOrEmpty(headerLine)) throw new ArgumentNullException("headerLine");

			this.HeaderLine = headerLine;
		}

		/// <summary>
		/// Gets the header line to match.
		/// </summary>
		public string HeaderLine { get; private set; }

		public override bool Matches(string fileName, byte[] header)
		{
			string headerLine = new StreamReader(new MemoryStream(header)).ReadLine();

			if (headerLine == this.HeaderLine)
				return true;

			return false;
		}
	}
}
